package com.moxiao.yfjh.module.blind.service.memberSeeme;

import com.moxiao.yfjh.module.blind.controller.app.controller.exchage.vo.AppMemberSeemeReqVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.memberSeeme.vo.MemberSeemeCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSeeme.vo.MemberSeemeExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSeeme.vo.MemberSeemePageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.memberSeeme.vo.MemberSeemeUpdateReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.dal.dataobject.memberSeeme.MemberSeemeDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.blind.convert.memberSeeme.MemberSeemeConvert;
import com.moxiao.yfjh.module.blind.dal.mysql.memberSeeme.MemberSeemeMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员看过我 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberSeemeServiceImpl implements MemberSeemeService {

    @Resource
    private MemberSeemeMapper memberSeemeMapper;

    @Override
    public Long createMemberSeeme(MemberSeemeCreateReqVO createReqVO) {
        // 插入
        MemberSeemeDO memberSeeme = MemberSeemeConvert.INSTANCE.convert(createReqVO);
        memberSeemeMapper.insert(memberSeeme);
        // 返回
        return memberSeeme.getId();
    }

    @Override
    public void updateMemberSeeme(MemberSeemeUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberSeemeExists(updateReqVO.getId());
        // 更新
        MemberSeemeDO updateObj = MemberSeemeConvert.INSTANCE.convert(updateReqVO);
        memberSeemeMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberSeeme(Long id) {
        // 校验存在
        validateMemberSeemeExists(id);
        // 删除
        memberSeemeMapper.deleteById(id);
    }

    private void validateMemberSeemeExists(Long id) {
        if (memberSeemeMapper.selectById(id) == null) {
            throw exception(MEMBER_SEEME_NOT_EXISTS);
        }
    }

    @Override
    public MemberSeemeDO getMemberSeeme(Long id) {
        return memberSeemeMapper.selectById(id);
    }

    @Override
    public List<MemberSeemeDO> getMemberSeemeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberSeemeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberSeemeDO> getMemberSeemePage(MemberSeemePageReqVO pageReqVO) {
        return memberSeemeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberSeemeDO> getMemberSeemeList(MemberSeemeExportReqVO exportReqVO) {
        return memberSeemeMapper.selectList(exportReqVO);
    }

    @Override
    public List<MemberSeemeDO> getMemberSeemeList(AppMemberSeemeReqVO appMemberSeemeReqVO) {
        return memberSeemeMapper.selectSeeMeList(appMemberSeemeReqVO);
    }

    @Override
    public MemberSeemeDO getMemberSeeme(String issue, String receive) {
        return memberSeemeMapper.selectOne(
                new LambdaQueryWrapper<MemberSeemeDO>().eq(MemberSeemeDO::getIssue, issue)
                .eq(MemberSeemeDO::getReceive, receive));
    }

    @Override
    public Long seeMeNum(Long userId) {
        LambdaQueryWrapper<MemberSeemeDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(MemberSeemeDO::getReceive,userId);
        return memberSeemeMapper.selectCount(lqw);
    }

}
